package com.yf.cmw.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.base.common.db.model.MachineCacheInfo;
import com.base.common.listener.SDCardFileObserver;
import com.yf.cmw.broadcast.AlarmReceiver;
import com.yf.cmw.constant.BussConstant;
import com.yf.cmw.presenter.AsyncCallback;
import com.yf.cmw.presenter.MainPresenter;

import java.util.Date;

/**
 * 后台任务Service
 */
public class TaskService extends Service implements AsyncCallback {

    static final String TAG = "TaskService";

    Context mContext;

    private MainPresenter mainPresenter = null;

    private static SDCardFileObserver sdCardFileObserver;

    public TaskService() {
        Log.e(TAG,"TaskService constructor invoke...");
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG,"onBind invoke...");
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mainPresenter.stopHttpServer();
      //  if(null != sdCardFileObserver) sdCardFileObserver.stopWatching();
    }

    @Override
    public void onCreate() {
        String msg = "TaskService onCreate invoke...";
        Log.e(TAG,msg);
        MainPresenter.writeLogRecord(msg);
        super.onCreate();
        mContext = this;
        this.mainPresenter = new MainPresenter(mContext,this);
//        initReceiver();
        this.mainPresenter.bussFlag = BussConstant.BUSS_FLAG_1; //获取机器配置
        this.mainPresenter.getMachineCfg();
        this.mainPresenter.startHttpServer();

        /*
        if(null == sdCardFileObserver){
            sdCardFileObserver = this.mainPresenter.getAdSourceFileObserver();
            sdCardFileObserver.startWatching();
            Log.i(TAG,"FileObserver 文件监控开始监听中...");
        }
        */
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG,"onStartCommand invoke...");
        if(null == this.mainPresenter){
            this.mainPresenter = new MainPresenter(mContext,this);
        }
        this.mainPresenter.bussFlag = BussConstant.BUSS_FLAG_1; //获取机器配置
        this.mainPresenter.getMachineCfg();
        return super.onStartCommand(intent, flags, startId);
    }



    @Override
    public void success(int flag, JSONObject result) {
        invokeByBussFlag(flag);
    }

    @Override
    public void failure(int flag, JSONObject result) {
        Log.e(TAG, result.getString("msg"));
        invokeByBussFlag(flag);
    }

    private void invokeByBussFlag(int flag){
        switch (flag){
            case BussConstant.BUSS_FLAG_1:{
                startAutoTask();
                break;
            }
        }
    }



    private void startAutoTask(){
        String msg = "startAutoTask invoke start ...";
        Log.e(TAG,msg);
        MainPresenter.writeLogRecord(msg);
        int time_interval = mainPresenter.getRoundTime();;
        Intent intent = new Intent(this,AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        // pendingIntent 为发送广播
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), pendingIntent);
        } else {
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(), time_interval, pendingIntent);
        }
        msg = "startAutoTask invoke end ...";
        Log.e(TAG,msg);
        //MainPresenter.writeLogRecord(msg);
    }

}
